﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;

namespace ArticleWeb.Common
{
    public static class UserTokenHelper
    {
        private static readonly string key = "dsahdyIuds124g2w&qjdsyum";

        private static readonly string iv = "GHs75&oo";

        public static Guid GetUserId(string token, string platform)
        {
            try
            {
                if (!string.IsNullOrEmpty(token) && string.IsNullOrEmpty(platform))
                {
                    var arr = CipherHelper.SymmetricDecrypt(token, CipherHelper.SymmetricFormat.TripleDES, key, iv).Split(',');
                    if (string.Compare(arr[1], platform, true) != 0) throw new Exception(HttpStatusCode.Unauthorized.ToString());

                    return Guid.Parse(arr[0]);
                }
            }
            catch
            {
            }
            return Guid.Empty;
        }
    }
}